/*   1:    */ package org.jetbrains.debugger.sourcemap;
/*   2:    */ 
/*   3:    */

import com.google.gson.stream.JsonToken;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Function;
import com.intellij.util.PathUtil;
import com.intellij.util.SmartList;
import com.intellij.util.text.CharSequenceSubSequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.io.JsonReaderEx;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */ 
/*  18:    */ public final class SourceMapDecoder
/*  19:    */ {
/*  20:    */   public static final int UNMAPPED = -1;
/*  21: 25 */   private static final Comparator<MappingEntry> a = new Comparator()
/*  22:    */   {
/*  23:    */     public int compare(MappingEntry paramAnonymousMappingEntry1, MappingEntry paramAnonymousMappingEntry2)
/*  24:    */     {
/*  25: 28 */       if (paramAnonymousMappingEntry1.getSourceLine() == paramAnonymousMappingEntry2.getSourceLine()) {
/*  26: 29 */         return paramAnonymousMappingEntry1.getSourceColumn() - paramAnonymousMappingEntry2.getSourceColumn();
/*  27:    */       }
/*  28: 32 */       return paramAnonymousMappingEntry1.getSourceLine() - paramAnonymousMappingEntry2.getSourceLine();
/*  29:    */     }
/*  30:    */   };
/*  31: 37 */   public static final Comparator<MappingEntry> MAPPING_COMPARATOR_BY_GENERATED_POSITION = new Comparator()
/*  32:    */   {
/*  33:    */     public int compare(MappingEntry paramAnonymousMappingEntry1, MappingEntry paramAnonymousMappingEntry2)
/*  34:    */     {
/*  35: 40 */       if (paramAnonymousMappingEntry1.getGeneratedLine() == paramAnonymousMappingEntry2.getGeneratedLine()) {
/*  36: 41 */         return paramAnonymousMappingEntry1.getGeneratedColumn() - paramAnonymousMappingEntry2.getGeneratedColumn();
/*  37:    */       }
/*  38: 44 */       return paramAnonymousMappingEntry1.getGeneratedLine() - paramAnonymousMappingEntry2.getGeneratedLine();
/*  39:    */     }
/*  40:    */   };
/*  41:    */   
/*  42:    */   public static SourceMap decode(@NotNull String paramString, @NotNull Function<List<String>, SourceResolver> paramFunction)
/*  43:    */     throws IOException
/*  44:    */   {
/*  45: 50 */     if (paramString == null) {
/*  46: 50 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "decode" }));
/*  47:    */     }
/*  48: 50 */     if (paramFunction == null) {
/*  49: 50 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "decode" }));
/*  50:    */     }
/*  51: 50 */     if (paramString.isEmpty()) {
/*  52: 51 */       throw new IOException("source map contents cannot be empty");
/*  53:    */     }
/*  54: 54 */     Object localObject = paramString;
/*  55: 55 */     if (paramString.startsWith(")]}")) {
/*  56: 56 */       localObject = new CharSequenceSubSequence(paramString, paramString.indexOf('\n') + 1, paramString.length());
/*  57:    */     }
/*  58: 58 */     return decode((CharSequence)localObject, paramFunction);
/*  59:    */   }
/*  60:    */   
/*  61:    */   @Nullable
/*  62:    */   public static SourceMap decode(@NotNull CharSequence paramCharSequence, @NotNull Function<List<String>, SourceResolver> paramFunction)
/*  63:    */     throws IOException
/*  64:    */   {
/*  65: 63 */     if (paramCharSequence == null) {
/*  66: 63 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "decode" }));
/*  67:    */     }
/*  68: 63 */     if (paramFunction == null) {
/*  69: 63 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "decode" }));
/*  70:    */     }
/*  71: 63 */     JsonReaderEx localJsonReaderEx = new JsonReaderEx(paramCharSequence);
/*  72: 64 */     ArrayList localArrayList = new ArrayList();
/*  73: 65 */     return a(localJsonReaderEx, 0, 0, localArrayList, paramFunction);
/*  74:    */   }
/*  75:    */   
/*  76:    */   @Nullable
/*  77:    */   private static SourceMap a(JsonReaderEx paramJsonReaderEx, int paramInt1, int paramInt2, List<MappingEntry> paramList, @NotNull Function<List<String>, SourceResolver> paramFunction)
/*  78:    */     throws IOException
/*  79:    */   {
/*  80: 74 */     if (paramFunction == null) {
/*  81: 74 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "4", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "parseMap" }));
/*  82:    */     }
/*  83: 74 */     paramJsonReaderEx.beginObject();
/*  84: 75 */     String str1 = null;
/*  85: 76 */     JsonReaderEx localJsonReaderEx = null;
/*  86: 77 */     Object localObject1 = null;
/*  87: 78 */     String str2 = null;
/*  88: 79 */     String str3 = null;
/*  89: 80 */     int i = -1;
/*  90: 81 */     while (paramJsonReaderEx.hasNext())
/*  91:    */     {
/*  92: 82 */       localObject2 = paramJsonReaderEx.nextName();
/*  93: 83 */       if (((String)localObject2).equals("sections")) {
/*  94: 84 */         throw new IOException("sections is not supported yet");
/*  95:    */       }
/*  96: 86 */       if (((String)localObject2).equals("version"))
/*  97:    */       {
/*  98: 87 */         i = paramJsonReaderEx.nextInt();
/*  99:    */       }
/* 100: 89 */       else if (((String)localObject2).equals("sourceRoot"))
/* 101:    */       {
/* 102: 90 */         str1 = a(paramJsonReaderEx);
/* 103:    */       }
/* 104: 92 */       else if (((String)localObject2).equals("sources"))
/* 105:    */       {
/* 106: 93 */         localJsonReaderEx = paramJsonReaderEx.subReader();
/* 107: 94 */         paramJsonReaderEx.skipValue();
/* 108:    */       }
/* 109: 96 */       else if (((String)localObject2).equals("names"))
/* 110:    */       {
/* 111: 97 */         paramJsonReaderEx.beginArray();
/* 112: 98 */         if (paramJsonReaderEx.hasNext())
/* 113:    */         {
/* 114: 99 */           localObject1 = new ArrayList();
/* 115:    */           do
/* 116:    */           {
/* 117:101 */             ((List)localObject1).add(paramJsonReaderEx.nextString(true));
/* 118:103 */           } while (paramJsonReaderEx.hasNext());
/* 119:    */         }
/* 120:    */         else
/* 121:    */         {
/* 122:106 */           localObject1 = Collections.emptyList();
/* 123:    */         }
/* 124:108 */         paramJsonReaderEx.endArray();
/* 125:    */       }
/* 126:110 */       else if (((String)localObject2).equals("mappings"))
/* 127:    */       {
/* 128:111 */         str2 = paramJsonReaderEx.nextString();
/* 129:    */       }
/* 130:113 */       else if (((String)localObject2).equals("file"))
/* 131:    */       {
/* 132:114 */         str3 = paramJsonReaderEx.nextString();
/* 133:    */       }
/* 134:    */       else
/* 135:    */       {
/* 136:118 */         paramJsonReaderEx.skipValue();
/* 137:    */       }
/* 138:    */     }
/* 139:121 */     paramJsonReaderEx.close();
/* 140:123 */     if (i != 3) {
/* 141:124 */       throw new IOException("Unsupported sourcemap version: " + i);
/* 142:    */     }
/* 143:127 */     if (localJsonReaderEx == null) {
/* 144:128 */       throw new IOException("sources is not specified");
/* 145:    */     }
/* 146:131 */     if (StringUtil.isEmpty(str2)) {
/* 147:133 */       return null;
/* 148:    */     }
/* 149:136 */     Object localObject2 = a(localJsonReaderEx, str1);
/* 150:    */     
/* 151:    */ 
/* 152:139 */     List[] arrayOfList = new List[((List)localObject2).size()];
/* 153:140 */     a(str2, paramInt1, paramInt2, paramList, arrayOfList, (List)localObject1);
/* 154:    */     
/* 155:142 */     MappingList[] arrayOfMappingList = new MappingList[arrayOfList.length];
/* 156:143 */     for (int j = 0; j < arrayOfList.length; j++)
/* 157:    */     {
/* 158:144 */       List localList = arrayOfList[j];
/* 159:145 */       if (localList != null)
/* 160:    */       {
/* 161:146 */         Collections.sort(localList, a);
/* 162:147 */         arrayOfMappingList[j] = new SourceMappingList(localList);
/* 163:    */       }
/* 164:    */     }
/* 165:150 */     return new SourceMap(str3, new GeneratedMappingList(paramList), arrayOfMappingList, (SourceResolver)paramFunction.fun(localObject2));
/* 166:    */   }
/* 167:    */   
/* 168:    */   @Nullable
/* 169:    */   private static String a(JsonReaderEx paramJsonReaderEx)
/* 170:    */   {
/* 171:155 */     return PathUtil.toSystemIndependentName(StringUtil.nullize(paramJsonReaderEx.nextString().trim()));
/* 172:    */   }
/* 173:    */   
/* 174:    */   private static void a(@NotNull String paramString, int paramInt1, int paramInt2, @NotNull List<MappingEntry> paramList, @NotNull List<MappingEntry>[] paramArrayOfList, @Nullable List<String> paramList1)
/* 175:    */   {
/* 176:164 */     if (paramString == null) {
/* 177:164 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "readMappings" }));
/* 178:    */     }
/* 179:164 */     if (paramList == null) {
/* 180:164 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "3", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "readMappings" }));
/* 181:    */     }
/* 182:164 */     if (paramArrayOfList == null) {
/* 183:164 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "4", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "readMappings" }));
/* 184:    */     }
/* 185:164 */     if (StringUtil.isEmpty(paramString)) {
/* 186:165 */       return;
/* 187:    */     }
/* 188:168 */     CharSequenceIterator localCharSequenceIterator = new CharSequenceIterator(paramString);
/* 189:169 */     int i = 0;
/* 190:170 */     List localList = a(paramArrayOfList, i);
/* 191:171 */     int j = 0;
/* 192:172 */     int k = 0;
/* 193:173 */     int m = 0;
/* 194:174 */     while (localCharSequenceIterator.hasNext())
/* 195:    */     {
/* 196:175 */       if (localCharSequenceIterator.peek() == ',') {
/* 197:176 */         localCharSequenceIterator.next();
/* 198:    */       } else {
/* 199:179 */         while (localCharSequenceIterator.peek() == ';')
/* 200:    */         {
/* 201:180 */           paramInt1++;
/* 202:181 */           paramInt2 = 0;
/* 203:182 */           localCharSequenceIterator.next();
/* 204:183 */           if (!localCharSequenceIterator.hasNext()) {
/* 205:184 */             return;
/* 206:    */           }
/* 207:    */         }
/* 208:    */       }
/* 209:189 */       paramInt2 += Base64VLQ.decode(localCharSequenceIterator);
/* 210:190 */       if (a(localCharSequenceIterator))
/* 211:    */       {
/* 212:191 */         paramList.add(new UnmappedEntry(paramInt1, paramInt2));
/* 213:    */       }
/* 214:    */       else
/* 215:    */       {
/* 216:195 */         int n = Base64VLQ.decode(localCharSequenceIterator);
/* 217:196 */         if (n != 0)
/* 218:    */         {
/* 219:197 */           i += n;
/* 220:198 */           localList = a(paramArrayOfList, i);
/* 221:    */         }
/* 222:200 */         j += Base64VLQ.decode(localCharSequenceIterator);
/* 223:201 */         k += Base64VLQ.decode(localCharSequenceIterator);
/* 224:    */         Object localObject;
/* 225:204 */         if (a(localCharSequenceIterator))
/* 226:    */         {
/* 227:205 */           localObject = new UnnamedEntry(paramInt1, paramInt2, i, j, k);
/* 228:    */         }
/* 229:    */         else
/* 230:    */         {
/* 231:208 */           m += Base64VLQ.decode(localCharSequenceIterator);
/* 232:209 */           assert (paramList1 != null);
/* 233:210 */           localObject = new NamedEntry((String)paramList1.get(m), paramInt1, paramInt2, i, j, k);
/* 234:    */         }
/* 235:212 */         localList.add(localObject);
/* 236:213 */         paramList.add(localObject);
/* 237:    */       }
/* 238:    */     }
/* 239:    */   }
/* 240:    */   
/* 241:    */   private static List<String> a(@NotNull JsonReaderEx paramJsonReaderEx, @Nullable String paramString)
/* 242:    */   {
/* 243:218 */     if (paramJsonReaderEx == null) {
/* 244:218 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "readSources" }));
/* 245:    */     }
/* 246:218 */     paramJsonReaderEx.beginArray();
/* 247:    */     Object localObject;
/* 248:220 */     if (paramJsonReaderEx.peek() == JsonToken.END_ARRAY)
/* 249:    */     {
/* 250:221 */       localObject = Collections.emptyList();
/* 251:    */     }
/* 252:    */     else
/* 253:    */     {
/* 254:224 */       localObject = new SmartList();
/* 255:    */       do
/* 256:    */       {
/* 257:226 */         String str = a(paramJsonReaderEx);
/* 258:227 */         str = paramString + '/' + str;
/* 259:228 */         ((List)localObject).add(str);
/* 260:230 */       } while (paramJsonReaderEx.hasNext());
/* 261:    */     }
/* 262:232 */     paramJsonReaderEx.endArray();
/* 263:233 */     return localObject;
/* 264:    */   }
/* 265:    */   
/* 266:    */   private static List<MappingEntry> a(@NotNull List<MappingEntry>[] paramArrayOfList, int paramInt)
/* 267:    */   {
/* 268:237 */     if (paramArrayOfList == null) {
/* 269:237 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder", "getMapping" }));
/* 270:    */     }
/* 271:237 */     Object localObject = paramArrayOfList[paramInt];
/* 272:238 */     if (localObject == null)
/* 273:    */     {
/* 274:239 */       localObject = new ArrayList();
/* 275:240 */       paramArrayOfList[paramInt] = localObject;
/* 276:    */     }
/* 277:242 */     return localObject;
/* 278:    */   }
/* 279:    */   
/* 280:    */   private static boolean a(CharSequenceIterator paramCharSequenceIterator)
/* 281:    */   {
/* 282:246 */     if (!paramCharSequenceIterator.hasNext()) {
/* 283:247 */       return true;
/* 284:    */     }
/* 285:250 */     int i = paramCharSequenceIterator.peek();
/* 286:251 */     return (i == 44) || (i == 59);
/* 287:    */   }
/* 288:    */   
/* 289:    */   private static class UnmappedEntry
/* 290:    */     extends MappingEntry
/* 291:    */   {
/* 292:    */     private final int a;
/* 293:    */     private final int b;
/* 294:    */     
/* 295:    */     UnmappedEntry(int paramInt1, int paramInt2)
/* 296:    */     {
/* 297:262 */       this.a = paramInt1;
/* 298:263 */       this.b = paramInt2;
/* 299:    */     }
/* 300:    */     
/* 301:    */     public int getGeneratedColumn()
/* 302:    */     {
/* 303:268 */       return this.b;
/* 304:    */     }
/* 305:    */     
/* 306:    */     public int getGeneratedLine()
/* 307:    */     {
/* 308:273 */       return this.a;
/* 309:    */     }
/* 310:    */     
/* 311:    */     public int getSourceLine()
/* 312:    */     {
/* 313:278 */       return -1;
/* 314:    */     }
/* 315:    */     
/* 316:    */     public int getSourceColumn()
/* 317:    */     {
/* 318:283 */       return -1;
/* 319:    */     }
/* 320:    */   }
/* 321:    */   
/* 322:    */   private static class UnnamedEntry
/* 323:    */     extends SourceMapDecoder.UnmappedEntry
/* 324:    */   {
/* 325:    */     private final int c;
/* 326:    */     private final int d;
/* 327:    */     private final int e;
/* 328:    */     
/* 329:    */     UnnamedEntry(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5)
/* 330:    */     {
/* 331:296 */       super(paramInt2);
/* 332:    */       
/* 333:298 */       this.c = paramInt3;
/* 334:299 */       this.d = paramInt4;
/* 335:300 */       this.e = paramInt5;
/* 336:    */     }
/* 337:    */     
/* 338:    */     public int getSource()
/* 339:    */     {
/* 340:305 */       return this.c;
/* 341:    */     }
/* 342:    */     
/* 343:    */     public int getSourceLine()
/* 344:    */     {
/* 345:310 */       return this.d;
/* 346:    */     }
/* 347:    */     
/* 348:    */     public int getSourceColumn()
/* 349:    */     {
/* 350:315 */       return this.e;
/* 351:    */     }
/* 352:    */   }
/* 353:    */   
/* 354:    */   private static class NamedEntry
/* 355:    */     extends SourceMapDecoder.UnnamedEntry
/* 356:    */   {
/* 357:    */     private final String f;
/* 358:    */     
/* 359:    */     NamedEntry(String paramString, int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5)
/* 360:    */     {
/* 361:326 */       super(paramInt2, paramInt3, paramInt4, paramInt5);
/* 362:327 */       this.f = paramString;
/* 363:    */     }
/* 364:    */     
/* 365:    */     public String getName()
/* 366:    */     {
/* 367:332 */       return this.f;
/* 368:    */     }
/* 369:    */   }
/* 370:    */   
/* 371:    */   private static class CharSequenceIterator
/* 372:    */     implements Base64VLQ.CharIterator
/* 373:    */   {
/* 374:    */     private final CharSequence a;
/* 375:    */     private final int b;
/* 376:340 */     private int c = 0;
/* 377:    */     
/* 378:    */     CharSequenceIterator(CharSequence paramCharSequence)
/* 379:    */     {
/* 380:343 */       this.a = paramCharSequence;
/* 381:344 */       this.b = paramCharSequence.length();
/* 382:    */     }
/* 383:    */     
/* 384:    */     public char next()
/* 385:    */     {
/* 386:349 */       return this.a.charAt(this.c++);
/* 387:    */     }
/* 388:    */     
/* 389:    */     char peek()
/* 390:    */     {
/* 391:353 */       return this.a.charAt(this.c);
/* 392:    */     }
/* 393:    */     
/* 394:    */     public boolean hasNext()
/* 395:    */     {
/* 396:358 */       return this.c < this.b;
/* 397:    */     }
/* 398:    */   }
/* 399:    */   
/* 400:    */   private static final class SourceMappingList
/* 401:    */     extends MappingList
/* 402:    */   {
/* 403:    */     public SourceMappingList(@NotNull List<MappingEntry> paramList)
/* 404:    */     {
/* 405:364 */       super();
/* 406:    */     }
/* 407:    */     
/* 408:    */     public int getLine(@NotNull MappingEntry paramMappingEntry)
/* 409:    */     {
/* 410:369 */       if (paramMappingEntry == null) {
/* 411:369 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder$SourceMappingList", "getLine" }));
/* 412:    */       }
/* 413:369 */       return paramMappingEntry.getSourceLine();
/* 414:    */     }
/* 415:    */     
/* 416:    */     public int getColumn(@NotNull MappingEntry paramMappingEntry)
/* 417:    */     {
/* 418:374 */       if (paramMappingEntry == null) {
/* 419:374 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder$SourceMappingList", "getColumn" }));
/* 420:    */       }
/* 421:374 */       return paramMappingEntry.getSourceColumn();
/* 422:    */     }
/* 423:    */     
/* 424:    */     protected Comparator<MappingEntry> getComparator()
/* 425:    */     {
/* 426:379 */       return SourceMapDecoder.a;
/* 427:    */     }
/* 428:    */   }
/* 429:    */   
/* 430:    */   private static final class GeneratedMappingList
/* 431:    */     extends MappingList
/* 432:    */   {
/* 433:    */     public GeneratedMappingList(List<MappingEntry> paramList)
/* 434:    */     {
/* 435:385 */       super();
/* 436:    */     }
/* 437:    */     
/* 438:    */     public int getLine(@NotNull MappingEntry paramMappingEntry)
/* 439:    */     {
/* 440:390 */       if (paramMappingEntry == null) {
/* 441:390 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder$GeneratedMappingList", "getLine" }));
/* 442:    */       }
/* 443:390 */       return paramMappingEntry.getGeneratedLine();
/* 444:    */     }
/* 445:    */     
/* 446:    */     public int getColumn(@NotNull MappingEntry paramMappingEntry)
/* 447:    */     {
/* 448:395 */       if (paramMappingEntry == null) {
/* 449:395 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/debugger/sourcemap/SourceMapDecoder$GeneratedMappingList", "getColumn" }));
/* 450:    */       }
/* 451:395 */       return paramMappingEntry.getGeneratedColumn();
/* 452:    */     }
/* 453:    */     
/* 454:    */     protected Comparator<MappingEntry> getComparator()
/* 455:    */     {
/* 456:400 */       return SourceMapDecoder.MAPPING_COMPARATOR_BY_GENERATED_POSITION;
/* 457:    */     }
/* 458:    */   }
/* 459:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.debugger.sourcemap.SourceMapDecoder

 * JD-Core Version:    0.7.0.1

 */